//
//  PersonCenterView.m
//  Inforcollection
//
//  Created by macbook on 15/12/3.
//  Copyright © 2015年 oceansoft.liw. All rights reserved.
//

#import "PersonCenterView.h"
#import "PersonIconCell.h"
#import "PersonOtherCell.h"

static NSString *iconCellID=@"PersonIconCell";
static NSString *otherCellID=@"PersonOtherCell";

static PersonCenterView *personView;

@implementation PersonCenterView
{
    NSArray *otherNameArray;
    
    NSArray *otherImageArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initViewWithFrame:frame];
    }
    return self;
}

-(void)initViewWithFrame:(CGRect)frame
{
    
    CGRect currentFrame=frame;
    
//    currentFrame.origin.y=20;
    
    _tableView=[[UITableView alloc]initWithFrame:currentFrame];
    
    _tableView.delegate=self;
    
    _tableView.dataSource=self;
    
    UINib *iconNib=[UINib nibWithNibName:@"PersonIconCell" bundle:nil];
    
    [_tableView registerNib:iconNib forCellReuseIdentifier:iconCellID];
    
    UINib *otherNib=[UINib nibWithNibName:@"PersonOtherCell" bundle:nil];
    
    [_tableView registerNib:otherNib forCellReuseIdentifier:otherCellID];
    
    _tableView.bounces=NO;
    
    [self setExtraCellLineHidden:_tableView];
    
    [self addSubview:_tableView];
    //@"设置" ,@"ico_sz_07@2x"
    otherNameArray=[NSArray arrayWithObjects:@"寄存管理",@"公告管理",@"个人中心",@"注销",nil];
    otherImageArray=[NSArray arrayWithObjects:@"ico_gezx_01",@"ico_gezx_02",@"ico_gezx_03",@"ico_gezx_06",nil];
}

+(instancetype)setFrame:(CGRect)frame WithModel:(UserModel *)model WihtLogin:(BOOL)isLogin WithCallBack:(StartCallBack)callBack
{
    personView = [[PersonCenterView alloc]initWithFrame:frame] ;
    
    personView.callBack=callBack;
    
    personView.userModel=model;
    
    personView.isLogin=isLogin;
    
    [personView.tableView reloadData];
    
    return personView;
}

-(void)reloadDataWithUserModel:(UserModel *)model WithLogin:(BOOL)isLogin
{
    _userModel=model;
    
    _isLogin=isLogin;
    
    [_tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0)
    {
           return 1;
    }
    else
    {
        if (_isLogin)
        {
            return otherNameArray.count;
        }
        else
        {
            return otherNameArray.count-1;
        }
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0)
    {
        PersonIconCell *cell=[tableView dequeueReusableCellWithIdentifier:iconCellID];
        if (cell==nil)
        {
            cell=[[PersonIconCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iconCellID];
        }
        if (_isLogin)
        {
//            [cell.iconImageView setImageURLStr:_userModel.icon placeholder:[UIImage imageNamed:KDefultIcon]];
            [cell.iconImageView setImage:[UIImage imageNamed:@"Person@2x.png"]];
            cell.userNameLabel.text=_userModel.name;
        }
        else
        {
            cell.iconImageView.image=[UIImage imageNamed:@"Person_icon@2x.png"];
            
            cell.userNameLabel.text=@"请登录";

        }
        return cell;
    }
    else
    {
        PersonOtherCell *cell=[tableView dequeueReusableCellWithIdentifier:otherCellID];
        if (cell==nil)
        {
            cell=[[PersonOtherCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:otherCellID];
        }
        cell.otherImageView.image=[UIImage imageNamed:otherImageArray[indexPath.row]];
        cell.otherImageView.highlightedImage=[UIImage imageNamed:[NSString stringWithFormat:@"%@_a",otherImageArray[indexPath.row]]];

        cell.nameLabel.text=otherNameArray[indexPath.row];
        
        cell.nameLabel.highlightedTextColor=AppNavColor;
        
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_callBack) {
        _callBack(indexPath);
    }
    if(indexPath.row==3){
        
    [SharedNotifiCenter postNotificationName:isUserlogOut object:nil];
        
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return 100;
    }
    else
    {
        return 44;
    }
}
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
}




@end
